package bb.lanxing.util.ui;

import android.content.Context;
import android.content.DialogInterface;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AlertDialog;

import bb.lanxing.R;
import kotlin.jvm.internal.Intrinsics;

public class CircularProgressDialog {
    private Context context;
    private ImageView imageDone;
    private AlertDialog materialDialog;
    private TextView messageView;
    private ProgressBar progress;

    public CircularProgressDialog(Context context, boolean cancelable, boolean outside, DialogInterface.OnCancelListener onCancelListener) {
        Intrinsics.checkNotNullParameter(context, "context");
        this.context = context;
        View inflate = LayoutInflater.from(context).inflate(R.layout.loading_progress_layout, null);
        this.messageView = inflate.findViewById(R.id.message_text);
        this.progress = inflate.findViewById(R.id.progress);
        this.imageDone = inflate.findViewById(R.id.img_done);
        this.materialDialog = new AlertDialog.Builder(context, R.style.AppTheme_Dialog).setView(inflate).setCancelable(cancelable).setOnCancelListener(onCancelListener).create();
//        DialogUtil.adjustDialogGravityCenter(create);
        AlertDialog alertDialog = this.materialDialog;
        Intrinsics.checkNotNull(alertDialog);
        alertDialog.setCanceledOnTouchOutside(outside);
    }

    public void show() {
        AlertDialog alertDialog = this.materialDialog;
        if (alertDialog != null) {
            alertDialog.show();
        }
    }

    public void show(CharSequence charSequence) {
        TextView textView = this.messageView;
        textView.setText(charSequence);
        TextView textView2 = this.messageView;
        textView2.setVisibility(TextUtils.isEmpty(charSequence) ? View.GONE : View.VISIBLE);
        show();
    }

    public void show(int i) {
        show(this.context.getString(i));
    }

    public void dismiss() {
        AlertDialog alertDialog = this.materialDialog;
        if (alertDialog != null) {
            alertDialog.dismiss();
        }
    }

    public void dismissWithDoneAnimation() {
        if (this.materialDialog != null) {
            if (progress == null) {
                return;
            }
            progress.clearAnimation();
            AlphaAnimation alphaAnimation = new AlphaAnimation(progress.getAlpha(), 0.0f);
            alphaAnimation.setDuration(300L);
            progress.startAnimation(alphaAnimation);
            progress.postDelayed(() -> {
//                    CircularProgressDialog.dismissWithDoneAnimation(CircularProgressDialog.this, r2);
            }, 300L);
        }
    }

    public static void dismissWithDoneAnimation(CircularProgressDialog this$0, long j) {
        ProgressBar progressBar = this$0.progress;
        if (progressBar != null) {
            progressBar.setVisibility(View.GONE);
        }
        ImageView imageView = this$0.imageDone;
        if (imageView != null) {
            imageView.setVisibility(View.VISIBLE);
        }
        ImageView imageView2 = this$0.imageDone;
        if (imageView2 != null) {
            imageView2.clearAnimation();
            ScaleAnimation scaleAnimation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, 1, 0.5f, 1, 0.5f);
            scaleAnimation.setDuration(j);
            imageView2.startAnimation(scaleAnimation);
        }
        ImageView imageView4 = this$0.imageDone;
        if (imageView4 != null) {
            imageView4.postDelayed(() -> {
//                    CircularProgressDialog.dismissWithDoneAnimation(CircularProgressDialog.this);
            }, j - 50);
        }
    }

    public static void dismissWithDoneAnimation(CircularProgressDialog d) {
        AlertDialog alertDialog = d.materialDialog;
        alertDialog.dismiss();
        ImageView imageView = d.imageDone;
        if (imageView != null) {
            imageView.setVisibility(View.GONE);
        }
        ProgressBar progressBar = d.progress;
        if (progressBar != null) {
            progressBar.setVisibility(View.VISIBLE);
        }
    }
}
